package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysLogininforDo;
import com.ruoyi.system.mapper.SysLogininforMapper;
import com.ruoyi.system.service.ISysLogininforService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 系统访问记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysLogininforServiceImpl implements ISysLogininforService {
    private final SysLogininforMapper sysLogininforMapper;

    /**
     * 查询系统访问记录
     */
    @Override
    public SysLogininforDo queryById(Long infoId) {
        return sysLogininforMapper.selectById(infoId);
    }

    /**
     * 查询系统访问记录列表
     */
    @Override
    public TableDataInfo<SysLogininforDo> queryPageList(SysLogininforDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysLogininforDo> lqw = buildQueryWrapper(entity);
        Page<SysLogininforDo> result = sysLogininforMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统访问记录列表
     */
    @Override
    public List<SysLogininforDo> queryList(SysLogininforDo entity) {
        LambdaQueryWrapper<SysLogininforDo> lqw = buildQueryWrapper(entity);
        return sysLogininforMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysLogininforDo> buildQueryWrapper(SysLogininforDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysLogininforDo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(entity.getUserName()), SysLogininforDo::getUserName, entity.getUserName());
        lqw.eq(StringUtils.isNotBlank(entity.getIpaddr()), SysLogininforDo::getIpaddr, entity.getIpaddr());
        lqw.eq(StringUtils.isNotBlank(entity.getLoginLocation()), SysLogininforDo::getLoginLocation, entity.getLoginLocation());
        lqw.eq(StringUtils.isNotBlank(entity.getBrowser()), SysLogininforDo::getBrowser, entity.getBrowser());
        lqw.eq(StringUtils.isNotBlank(entity.getOs()), SysLogininforDo::getOs, entity.getOs());
        lqw.eq(StringUtils.isNotBlank(entity.getStatus()), SysLogininforDo::getStatus, entity.getStatus());
        lqw.eq(StringUtils.isNotBlank(entity.getMsg()), SysLogininforDo::getMsg, entity.getMsg());
        lqw.eq(entity.getLoginTime() != null, SysLogininforDo::getLoginTime, entity.getLoginTime());
        return lqw;
    }

    /**
     * 新增系统访问记录
     */
    @Override
    public Boolean insert(SysLogininforDo entity) {
        SysLogininforDo add = BeanUtil.toBean(entity, SysLogininforDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysLogininforMapper.insert(add) > 0;
        if (flag) {
            entity.setInfoId(add.getInfoId());
        }
        return flag;
    }

    /**
     * 修改系统访问记录
     */
    @Override
    public Boolean updateById(SysLogininforDo entity) {
        SysLogininforDo update = BeanUtil.toBean(entity, SysLogininforDo.class);
        validEntityBeforeSave(update);
        return sysLogininforMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysLogininforDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除系统访问记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysLogininforMapper.deleteBatchIds(ids) > 0;
    }
}
